

package cn.edu.scujcc;

import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class NoteService {
	@Autowired
	private NoteRepository repo;
	
	public Note createNote(Note n) {
		Note result = null;
		result = repo.save(n);
		return result;
	}
	public Note updateNote(Note n) {
		Note result = null;
		result = getNote(n.getleave_name());
		if (result != null) {
			if (n.getleave_name() != null) {
				result.setLeave_name(n.getleave_name());
			}
			if (n.getLeave_time() != null) {
				result.setLeave_time(n.getLeave_time());
			}
			if (n.getLeave_reason() != null) {
				result.setLeave_reason(n.getLeave_reason());
			}
			result = repo.save(result);
		}
		
		return result;
	}
	
	public List<Note> getAllNote() {
		List<Note> result = null;
		result = repo.findAll();
		return result;
	}
	
	public Note getNote(User leave_name) {
		Note result = null;
		Optional<Note> op = repo.findByleave_name(leave_name);
		if (op.isPresent()) {
			result = op.get();
		}
		return result;
	}
	
	public boolean deleteNote(User leave_name) {
//		boolean result = false;
//		try {
//			repo.deleteById(id);
//			result = true;
//		} catch (Exception c) {
//			result = false;
//		}
		boolean result = true;
		try {
			repo.deleteAll();
		} catch (Exception n) {
			result = false;
		}
		return result;
	}
	
	public Optional<Note> searchNote(User leave_name){
		Optional<Note> result = null;
		result = repo.findByleave_name(leave_name);
		return result;
	}
}
